import store from '@/store'
import tools from './tools'
import { sendSock } from './socketSend'
import { aesDecrypt } from '@/secret/secret'
import { setDataDecryptController } from './socketOnMessageTools'

// 解析消息
export const decryptMessage = (body, nameArr, firstIndex) => {
  const msgData = aesDecrypt(body, store.state.socket.DHSecretKey, true).buffer
  setDataDecryptController({
    bodyData: msgData,
    nameArr,
    firstIndex
  })
}

// 判断缓存用户列表中是否有该用户
export const ifContactInList = async (id, idType) => {
  if (idType === 1) {
    if (!store.state.chatInformation.userList[id]) {
      const userDetail = await store.dispatch('addressList/getUserDetail', id)
      if (userDetail.data && userDetail.data.code === 200) {
        const userImgId = userDetail.data.data.info.user_img
        const userName = userDetail.data.data.info.user_name
        const remarks = userDetail.data.data.info.remarks
        const userPhone = userDetail.data.data.info.user_phone
        const contact = userDetail.data.data.in_contact
        store.commit('chatInformation/add_user', {
          userID: id,
          userInfo: {
            user_name: userName,
            user_img_id: userImgId,
            in_contact: contact,
            remarks,
            user_phone: userPhone
          }
        })
      }
    }
  } else {
    if (!store.state.chatInformation.groupList[id]) {
      const groupDetail = await store.dispatch('groups/getGroupInfo', id)
      if (groupDetail.data && groupDetail.data.data && groupDetail.data.code === 200) {
        const group = {
          group_name: groupDetail.data.data.group_name,
          group_img_id: groupDetail.data.data.group_img,
          group_sponsor: groupDetail.data.data.group_sponsor
        }
        store.commit('chatInformation/add_group', {
          groupID: groupDetail.data.data.group_id,
          groupInfo: group
        })
      }
    }
  }
}

// 处理消息
export const dealMessage = async (msgObj, idType, isGroup) => {
  delete msgObj.msg_length
  // 如果现有联系人列表中没有当前联系人，则添加该联系人并置顶，否则置顶已有联系人
  if (store.state.chatInformation.chatInformation.every(item => !(item.id === (!isGroup ? msgObj.id : msgObj.group_id) && item.id_type === idType))) {
    const contactInfo = await tools.getContactInfo(!isGroup ? msgObj.id : msgObj.group_id, idType)
    const contact = {
      id_type: idType,
      messages: [],
      time: msgObj.timestamp,
      id: !isGroup ? msgObj.id : msgObj.group_id,
      name: idType === 1 ? contactInfo.user_name : contactInfo.group_name,
      draft: ''
    }
    if (idType === 1) {
      contact.in_contact = tools.isFriend(msgObj.id)
    }
    store.commit('chatInformation/set_topContact', contact)
  } else {
    store.commit('chatInformation/set_topContact', {
      id: isGroup ? msgObj.group_id : msgObj.id,
      id_type: idType
    })
  }
  // 添加一条消息至该联系人
  if (store.state.chatInformation.current_contact && store.state.chatInformation.current_contact.id === (isGroup ? msgObj.group_id : msgObj.id) && store.state.chatInformation.current_contact.id_type === idType) {
    store.dispatch('chatInformation/setAddMessage', {
      message: {
        ...msgObj,
        unread: false
      },
      id: isGroup ? msgObj.group_id : msgObj.id,
      idType
    })
    sendSock({
      type: '5.2.1',
      body: {
        msg_id: msgObj.msg_id
      }
    })
  } else {
    store.dispatch('chatInformation/setAddMessage', {
      message: {
        ...msgObj,
        unread: true
      },
      id: isGroup ? msgObj.group_id : msgObj.id,
      idType
    })
  }
}
